package com.gugee.ins.data.task.blogger.service;

import com.gugee.common.task.dto.TaskParam;
import com.gugee.ins.common.constant.InsBloggerMsgTypeCodeEnum;
import com.gugee.ins.common.dal.service.blogger.InsBloggerRelationDao;
import com.gugee.ins.data.common.msg.InsBloggerMessage;
import com.gugee.ins.data.task.config.TaskConfig;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 如果有多个类似需求，可以再做模板设计
 */
@Component
@Slf4j
public class InsCollectBloggerRelationSvc  {
    @Autowired
    InsBloggerRelationDao insBloggerRelationDao;
    @Autowired
    TaskConfig taskConfig;
    @Autowired
    @Qualifier("insBloggerMsgBaseInfoProducer")
    private KafkaTemplate<Long, InsBloggerMessage> insBloggerMsgBaseInfoProducer;
    public ReturnT<String> xxlJobStart(Integer taskType, String who, TaskParam taskParam){
        long start = System.currentTimeMillis();
        try {
            log.info("{}:开始准备执行任务: taskParam={}",who, taskParam);
            XxlJobLogger.log("{}:开始启动消息投递任务: taskParam={}", who,taskParam);
            if (taskType!=null ) {
                run(taskType, who, taskParam);
            }

            return ReturnT.SUCCESS;
        } catch (Exception ex) {
            XxlJobLogger.log(ex);
            ex.printStackTrace();
            return ReturnT.FAIL;
        }finally {
            log.info("{}:执行结束  end cost total={},",who, (System.currentTimeMillis() - start) / 1000);
            XxlJobLogger.log("{}:执行结束  end cost total={}, params={}", who,(System.currentTimeMillis() - start) / 1000,taskParam);
        }
    }

    public void run(int taskType, String who,TaskParam taskParam) {
        long startTime = 0L;
        long endTime = 0L;
        //表示startPage为最近N小时
        if(0==taskParam.getPagesLimit()){
            startTime = System.currentTimeMillis()/1000 - taskParam.getStartPage()*3600;
            endTime = System.currentTimeMillis()/1000;
        }else{
            startTime = taskParam.getStartPage();
            endTime = taskParam.getPagesLimit();
        }

        if(endTime<=startTime){
            log.error("{}:输入参数有问题:{},",who,taskParam);
            return ;
        }
        executeTask(taskParam,  who, startTime, endTime);
    }

    public void executeTask(TaskParam taskParam, String who,long startTime,long endTime) {

        List<InsBloggerMessage>  insBloggerMessages=insBloggerRelationDao.produceMsgFromBloggerRelation(startTime,endTime);
        //未收录过（uid未爬取成功过的）播主消息，需设置重试次数来加快收录
        for(InsBloggerMessage msg:insBloggerMessages){
            msg.setRetryLimitTimes(taskConfig.getRetryBloggerTimes());
            msg.setTypeCode(InsBloggerMsgTypeCodeEnum.UID_UPDATE_F_MBLOGGER_RELATION_TIMING.getCode());
            insBloggerMsgBaseInfoProducer.send(taskParam.getTopic(),msg);
        }
    }
}
